package com.cqjtu.eecs.utils.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cqjtu.eecs.entity.TrainingGoalTemplate;
import com.cqjtu.eecs.entity.dto.TrainingGoalDTO;
import com.cqjtu.eecs.service.ITrainingGoalTemplateService;
import com.cqjtu.eecs.service.impl.TrainingGoalTemplateServiceImpl;
import org.apache.catalina.LifecycleState;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 培养目标excel监听器
 * @author: 徐文升
 * @create: 2021-12-12 14:56
 **/

public class TrainingGoalListener extends AnalysisEventListener<TrainingGoalDTO> {
    /**
     * 专业代码
     */
    private String majorCode;

    /**
     * 版本编号
     */
    private String versionCode;

    private ITrainingGoalTemplateService trainingGoalTemplateService;

    private List<TrainingGoalTemplate> trainingGoalTemplates = new ArrayList<>();

    public TrainingGoalListener(String majorCode, String versionCode,
                                ITrainingGoalTemplateService trainingGoalTemplateService) {
        this.majorCode = majorCode;
        this.versionCode = versionCode;
        this.trainingGoalTemplateService = trainingGoalTemplateService;
    }


    @Override
    public void invoke(TrainingGoalDTO trainingGoalDTO, AnalysisContext analysisContext) {
        if (trainingGoalDTO.getTrainingGoalCode() != null)
            trainingGoalTemplates.add(new TrainingGoalTemplate(null, trainingGoalDTO.getTrainingGoalCode(),
                    majorCode, versionCode, trainingGoalDTO.getTrainingGoalDescription()));
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        trainingGoalTemplateService.saveBatch(trainingGoalTemplates);
        trainingGoalTemplates.clear();
    }
}
